package com.android.PopFax;
import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 


public class ExampleHandler extends DefaultHandler{ 

	// =========================================================== 
	// Fields 
	// =========================================================== 

	private boolean in_outertag = false; 
	private boolean in_innertag = false; 
	private boolean in_mytagError = false;
	private boolean in_faxlisttag = false;
	private boolean in_faxtag = false;
	private boolean in_faxidtag = false;
	private boolean in_fromtag = false;
	private boolean in_datetag = false;
	private boolean in_pagestag = false;
	private boolean in_mytagResult = false;
	private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet(); 

	// =========================================================== 
	// Getter & Setter 
	// =========================================================== 

	public ParsedExampleDataSet getParsedData() { 
		return this.myParsedExampleDataSet; 
	} 

	// =========================================================== 
	// Methods 
	// =========================================================== 
	@Override 
	public void startDocument() throws SAXException { 
		this.myParsedExampleDataSet = new ParsedExampleDataSet(); 
	} 

	@Override 
	public void endDocument() throws SAXException { 
		// Nothing to do 
	} 

	/** Gets be called on opening tags like: 
	 * <tag> 
	 * Can provide attribute(s), when xml was like: 
	 * <tag attribute="attributeValue">*/ 
	@Override 
	public void startElement(String namespaceURI, String localName, 
			String qName, Attributes atts) throws SAXException { 
		if (localName.equals("API_RESULT")) { 
			this.in_outertag = true; 
		}else if (localName.equals("AUTHENTICATION")) { 
			this.in_innertag = true; 
		}else if (localName.equals("RESULT") ) { 
			this.in_mytagResult = true; 
		}
		else if (localName.equals("RETRIEVE_INCOMING_FAX_LIST") ) { 
			this.in_faxlisttag = true; 
		}
		else if (localName.equals("FAX") ) { 
			this.in_faxtag = true; 
		}
		else if (localName.equals("FAX_ID") ) { 
			this.in_faxidtag = true; 
		}
		else if (localName.equals("FROM") ) { 
			this.in_fromtag = true; 
		}
		else if (localName.equals("DATE") ) { 
			this.in_datetag = true; 
		}else if (localName.equals("PAGES") ) { 
			this.in_pagestag = true; 
		}
		else if (localName.equals("ERROR") ) { 
			this.in_mytagError = true; 
		}
	}
	/** Gets be called on closing tags like: 
	 * </tag> */ 
	@Override 
	public void endElement(String namespaceURI, String localName, String qName) 
	throws SAXException { 
		if (localName.equals("API_RESULT")) { 
			this.in_outertag = false; 
		}else if (localName.equals("AUTHENTICATION")) { 
			this.in_innertag = false; 
		}else if (localName.equals("RESULT") ) { 
			this.in_mytagResult = false;      
		}
		else if (localName.equals("RETRIEVE_INCOMING_FAX_LIST") ) { 
			this.in_faxlisttag = false; 
		}
		else if (localName.equals("FAX") ) { 
			
			myParsedExampleDataSet.rowWiseDataBindIntoHashMap();
			
			this.in_faxtag = false; 
		}
		else if (localName.equals("FAX_ID") ) { 
			this.in_faxidtag = false; 
		}
		else if (localName.equals("FROM") ) { 
			this.in_fromtag = false; 
		}
		else if (localName.equals("DATE") ) { 
			this.in_datetag = false; 
		}else if (localName.equals("PAGES") ) { 
			this.in_pagestag = false; 
		}
		else if (localName.equals("ERROR") ) { 
			this.in_mytagError = false; 
		}
	} 

	/** Gets be called on the following structure: 
	 * <tag>characters</tag> */ 
	@Override 
	public void characters(char ch[], int start, int length) { 
		if(this.in_mytagResult){ 
			myParsedExampleDataSet.setExtractedInt(Integer.parseInt((new String(ch, start, length)))); 
		}
		else if (this.in_faxidtag){ 
			myParsedExampleDataSet.setFax_id(new String(ch, start, length));
		}
		else if (this.in_fromtag){ 
			myParsedExampleDataSet.setFax_from(new String(ch, start, length)); 
		}
		else if (this.in_datetag){ 
			myParsedExampleDataSet.setFax_date(new String(ch, start, length)); 
		}
		else if (this.in_pagestag){
			myParsedExampleDataSet.setFax_pages(new String(ch, start, length)); 
		}
		else if (this.in_mytagError){ 
			myParsedExampleDataSet.setExtractedInt(Integer.parseInt((new String(ch, start, length)))); 
		}
	} 
}

